package com.qianfeng.gl4study.youshare.acitivity.common;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.acitivity.store.StoreActivity;
import com.qianfeng.gl4study.youshare.tasks.LoginTask;
import com.qianfeng.gl4study.youshare.tasks.TaskProcessor;
import com.qianfeng.gl4study.youshare.utils.Constants;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/24
 * Email:pdsfgl@live.com
 */
public class LoginActivity extends Activity implements View.OnClickListener, TaskProcessor {

	private EditText userName;
	private EditText userPassword;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		displayActionBar();
		userName = (EditText) findViewById(R.id.login_name_user);
		userPassword = (EditText) findViewById(R.id.login_password_user);
		Button login = (Button) findViewById(R.id.login_button_login);
		login.setOnClickListener(this);
		Button register = (Button) findViewById(R.id.login_button_register);
		register.setOnClickListener(this);
	}

	/**
	 * 点击登录或注册按钮调用的监听的方法
	 * @param view
	 */
	@Override
	public void onClick(View view) {
		int id = view.getId();
		if(id == R.id.login_button_login){
			String userNameString = userName.getText().toString().trim();
			String passwordString = userPassword.getText().toString().trim();
			if(null==userNameString||userNameString.equals("")){
				//提示用户名不能为空
				Toast.makeText(this,"用户名不能为空",Toast.LENGTH_LONG).show();
			}else if(null==passwordString||passwordString.equals("")){
				//提示密码不能为空
				Toast.makeText(this,"密码不能为空",Toast.LENGTH_LONG).show();
			}else {
				//开启异步进行联网进行登录操作
				LoginTask task = new LoginTask(this);
				task.execute(userNameString,passwordString);
			}
		}else if(id == R.id.login_button_register){
			Intent intent = new Intent(this,RegisterActivity.class);
			startActivity(intent);
		}
	}

	/**
	 * ActionBar的显示实现方法
	 */
	private void displayActionBar() {
		//去掉ActionBar的图标，标题居中
		ActionBar actionBar = getActionBar();
		if (actionBar != null) {
			actionBar.setDisplayHomeAsUpEnabled(true);
			actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));
			actionBar.setDisplayShowCustomEnabled(true);
			LayoutInflater inflater = LayoutInflater.from(this);
			View view = inflater.inflate(R.layout.actionbar_main_title, null);
			// 获取view中的 TextView，进行标题的设置
			TextView txtTitle = (TextView) view.findViewById(R.id.actionbar_title);
			if (txtTitle != null) {
				txtTitle.setText("登录");
				txtTitle.setTextColor(Color.WHITE);
			}
			actionBar.setCustomView(
				view,
				new ActionBar.LayoutParams(
						ViewGroup.LayoutParams.MATCH_PARENT,
						ViewGroup.LayoutParams.WRAP_CONTENT,
						Gravity.CENTER
				)
			);
		}
	}

	@Override
	public void processResult(JSONObject result, int flag) {

		if(result!=null) {
			String resultString = "登录失败";
			try {
				int code = result.getInt("code");
				//code为1，代表返回数据正确，msg是一个对象，若code不是1，代表数据请求失败，msg返回字符串
				if (code == 1) {
					String userId = result.getJSONObject("msg").getString("user_id");
					if (userId == null || "".equals(userId)) {
						String sessionid = result.getJSONObject("msg").getString("sessionid");
						resultString += "原因：" + sessionid;
						Toast.makeText(this, resultString, Toast.LENGTH_LONG).show();
					} else {
						resultString = "登录成功";
						Constants.USER_ID = userId;
						Toast.makeText(this, resultString, Toast.LENGTH_LONG).show();
						Intent intent = new Intent(this, StoreActivity.class);
						startActivity(intent);
						finish();
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
}